package com.product.android.ui.waterfall;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class LazyScrollView extends ScrollView {	
	private final int ACTION_UP_ID = -9983762;
	private int miLastScrollY = 0;
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
//		if(computeVerticalScrollOffset ()){
			
//		}
		
//		if(Math.abs(t-oldt)>500){
//			Log.i(TAG,String.format("l=%s, t=%s, oldl=%s, oldt=%s", l, t, oldl, oldt));
//		}
//		else{
//			Log.d(TAG,String.format("l=%s, t=%s, oldl=%s, oldt=%s", l, t, oldl, oldt));
//		}
		onScrollListener.onAutoScroll(l, t, oldl, oldt);
		
	}
	
	 public void fling(int velocityY) {
         super.fling(velocityY*2 );
     }
	
	private static final String TAG = "LazyScrollView";
	private View view;

	public LazyScrollView(Context context){
		super(context);
	}
	

	public LazyScrollView(Context context,AttributeSet attrs){
		super(context, attrs);
	}

	public LazyScrollView(Context context,AttributeSet attrs,int defStyle){
		super(context, attrs, defStyle);
	}
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg){
			super.handleMessage(msg);
			switch (msg.what){
			case 1:
				if (view.getMeasuredHeight() - 20 <= getScrollY() + getHeight()) {
					if (onScrollListener != null){
						onScrollListener.onBottom();
					}
				} else if (getScrollY() == 0){
					if (onScrollListener != null){
						onScrollListener.onTop();
					}
				} else {
					if (onScrollListener != null) {
						onScrollListener.onScroll();
					}
				}
				break;
			case ACTION_UP_ID:
				View scroller = (View) msg.obj;
				if(null != scroller){
					if (miLastScrollY == scroller.getScrollY()) {
						onScrollListener.onStop();
					} else {
						handler.sendMessageDelayed(
								handler.obtainMessage(ACTION_UP_ID, scroller), 50);
						miLastScrollY = scroller.getScrollY();
					}
				}
				break;
			default:
				break;
			}
		}
	};

	private void init() {
		this.setOnTouchListener(onTouchListener);
	}

	OnTouchListener onTouchListener = new OnTouchListener(){
		@Override
		public boolean onTouch(View v, MotionEvent event){
			switch (event.getAction()){
			case MotionEvent.ACTION_MOVE:
				if (view != null && onScrollListener != null){
					onScrollListener.onMove();
				}
				break;
			case MotionEvent.ACTION_UP:
				if (view != null && onScrollListener != null){
					handler.sendMessageDelayed(handler.obtainMessage(1), 200);
					onScrollListener.onUp();
					handler.sendMessageDelayed(
							handler.obtainMessage(ACTION_UP_ID, v), 50);
				}
				break;
			}
			return false;
		}
	};
	

	/**
	 * 获得参考的View，主要是为了获得它的MeasuredHeight，然后和滚动条的ScrollY+getHeight作比较。
	 */
	public void getView() {
		this.view = getChildAt(0);
		if (view != null) {
			init();
		}
	}
	
	float startY =0f;
	float endY =0f;
	float diffY = 0f;
	
	
//	@Override
//	public boolean onInterceptTouchEvent(MotionEvent e){
//		switch(e.getAction()){
//		case MotionEvent.ACTION_DOWN:
//			startY = e.getY();
//			break;
//		case MotionEvent.ACTION_UP:
//			endY = e.getY();
//		
//			diffY = Math.abs(startY- endY);
//			if(diffY>50){
//				return true;
//			}
//			break;
//		}
//		onTouchEvent(e);
//		return false;
//	};


	/**
	 * 定义接口
	 */
	public interface OnScrollListener {
		void onMove();
		void onUp();
		void onBottom();
		void onTop();
		void onScroll();
		void onAutoScroll(int l, int t, int oldl, int oldt);
		void onStop();
	}

	private OnScrollListener onScrollListener;

	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}
}